package com.czl.module_service.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import com.alibaba.android.arouter.facade.annotation.Route
import com.bigkoo.pickerview.view.TimePickerView
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.czl.base.base.BaseActivity
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.HealthReportBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DialogHelper
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.databinding.ActivityAddFamilyHealthBinding
import com.czl.module_service.viewmodel.AddFamilyHealthViewModel
import com.lxj.xpopup.util.KeyboardUtils
import java.util.*

@Route(path = AppConstants.Router.Service.F_ADD_FAMILY_HEALTH)
class AddFamilyHealthActivity : BaseActivity<ActivityAddFamilyHealthBinding, AddFamilyHealthViewModel>() {

    private lateinit var pvTime: TimePickerView

    var relation = HealthReportBean.FamilyData()

    var lastRadioButton: RadioButton? = null
    var lastRadioButton1: RadioButton? = null

    override fun initContentView(): Int {
        return R.layout.activity_add_family_health
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("添加亲属")
        // 跳转亲属上报
        binding.btnAddRelation.setOnClickListener {
            relation.relativesName = binding.etName.text.toString()
            relation.relativesCard = binding.etIdCard.text.toString()
            relation.relativesPhone = binding.etPhone.text.toString()
            relation.temperature = binding.etTemp.text.toString()
            if (relation.healthyType == 0 || relation.healthyType == 4 || relation.healthyType == 5) {
                relation.admitted = null
                relation.confirmed = null
            }
            if (relation.healthyType == 1) {
                relation.suspected = ""
                relation.confirmed = null
            }
            if (relation.healthyType == 3) {
                relation.suspected = ""
            }
            // 点击疑似
            if (relation.healthyType == 2) {
                // 疑似症状
                val suspected = mutableListOf <Any>()
                if (binding.cb1.isChecked) suspected.add(binding.cb1.text as String)
                if (binding.cb2.isChecked) suspected.add(binding.cb2.text as String)
                if (binding.cb3.isChecked) suspected.add(binding.cb3.text as String)
                if (binding.cb4.isChecked) suspected.add(binding.cb4.text as String)
                relation.suspected = ""
                relation.suspected = TextUtils.join("、", suspected)
            }
            if (relation.touching == 1) {
                relation.touchingTime = binding.tvTouchTime.text.toString()
                relation.touchingName = binding.tvTouchName.text.toString()
                relation.touchingPhone = binding.tvTouchPhone.text.toString()
            }
            viewModel.addFamilyHealthyReport(relation)
        }
        // 亲属上报内容
        // 亲属关系
        var list = arrayListOf<RadioButton>()
        for (index in 1..binding.llRg1.childCount){
            list.add(binding.llRg1.getChildAt(index -1) as RadioButton)
        }
        for (index in 1..binding.llRg2.childCount){
            list.add(binding.llRg2.getChildAt(index -1) as RadioButton)
        }
        for (v in list) {
            lastRadioButton = list[0]
            v.setOnClickListener{v1: View ->
                KeyboardUtils.hideSoftInput(v)
                if (lastRadioButton?.id != v1.id) {
                    lastRadioButton?.apply {
                        isChecked = false
                    }
                }
                lastRadioButton = v
                if (v.id == R.id.rb1) relation.relativesType = 0 else if (v.id == R.id.rb2) relation.relativesType = 1 else if (v.id == R.id.rb3) relation.relativesType = 2 else relation.relativesType = 3
            }
        }
        // 健康状况
        var list1 = arrayListOf<RadioButton>()
        for (index in 1..binding.llRg3.childCount){
            list1.add(binding.llRg3.getChildAt(index -1) as RadioButton)
        }
        for (index in 1..binding.llRg4.childCount){
            list1.add(binding.llRg4.getChildAt(index -1) as RadioButton)
        }
        for (v in list1) {
            lastRadioButton1 = list1[0]
            v.setOnClickListener{v1: View ->
                KeyboardUtils.hideSoftInput(v)
                if (lastRadioButton1?.id != v1.id) {
                    lastRadioButton1?.apply {
                        isChecked = false
                    }
                }
                lastRadioButton1 = v
                if (v.id == R.id.rb5 || v.id == R.id.rb9 || v.id == R.id.rb10) {
                    if (v.id == R.id.rb5) relation.healthyType = 0 else if (v.id == R.id.rb9) relation.healthyType = 4 else relation.healthyType = 5
                    findViewById<LinearLayout>(R.id.ll_visibility1).visibility = View.GONE
                    findViewById<LinearLayout>(R.id.ll_visibility2).visibility = View.GONE
                    findViewById<LinearLayout>(R.id.ll_visibility3).visibility = View.GONE
                }
                if (v.id == R.id.rb6) {
                    relation.healthyType = 1
                    findViewById<LinearLayout>(R.id.ll_visibility1).visibility = View.VISIBLE
                    findViewById<LinearLayout>(R.id.ll_visibility2).visibility = View.GONE
                    findViewById<LinearLayout>(R.id.ll_visibility3).visibility = View.GONE
                    val rg3 = findViewById<RadioGroup>(R.id.rg5)
                    relation.admitted = 1
                    rg3.setOnCheckedChangeListener { group, checkedId ->
                        KeyboardUtils.hideSoftInput(rg3)
                        val checkedRadioButton = group?.findViewById(group.checkedRadioButtonId) as? RadioButton
                        checkedRadioButton?.let {
                            if (checkedRadioButton.isChecked) {
                                if (checkedRadioButton.id == R.id.rb11) {
                                    relation.admitted = 1
                                } else if (checkedRadioButton.id == R.id.rb12) {
                                    relation.admitted = 0
                                }
                            }

                        }
                    }
                }
                if (v.id == R.id.rb7) {
                    relation.healthyType = 2
                    findViewById<LinearLayout>(R.id.ll_visibility1).visibility = View.GONE
                    findViewById<LinearLayout>(R.id.ll_visibility2).visibility = View.VISIBLE
                    findViewById<LinearLayout>(R.id.ll_visibility3).visibility = View.GONE
                    val rg4 = findViewById<RadioGroup>(R.id.rg6)
                    relation.admitted = 1
                    rg4.setOnCheckedChangeListener { group, checkedId ->
                        KeyboardUtils.hideSoftInput(rg4)
                        val checkedRadioButton = group?.findViewById(group.checkedRadioButtonId) as? RadioButton
                        checkedRadioButton?.let {
                            if (checkedRadioButton.isChecked) {
                                if (checkedRadioButton.id == R.id.rb13) {
                                    relation.admitted = 1
                                } else if (checkedRadioButton.id == R.id.rb14) {
                                    relation.admitted = 0
                                }
                            }

                        }
                    }
                }
                if (v.id == R.id.rb8) {
                    relation.healthyType = 3
                    findViewById<LinearLayout>(R.id.ll_visibility1).visibility = View.GONE
                    findViewById<LinearLayout>(R.id.ll_visibility2).visibility = View.GONE
                    findViewById<LinearLayout>(R.id.ll_visibility3).visibility = View.VISIBLE
                    val rg5 = findViewById<RadioGroup>(R.id.rg7)
                    relation.admitted = 1
                    rg5.setOnCheckedChangeListener { group, checkedId ->
                        KeyboardUtils.hideSoftInput(rg5)
                        val checkedRadioButton = group?.findViewById(group.checkedRadioButtonId) as? RadioButton
                        checkedRadioButton?.let {
                            if (checkedRadioButton.isChecked) {
                                if (checkedRadioButton.id == R.id.rb15) {
                                    relation.admitted = 1
                                } else if (checkedRadioButton.id == R.id.rb16) {
                                    relation.admitted = 0
                                }
                            }

                        }
                    }
                    // 确诊症状
                    val rg6 = findViewById<RadioGroup>(R.id.rg8)
                    relation.confirmed = 1
                    rg6.setOnCheckedChangeListener { group, checkedId ->
                        KeyboardUtils.hideSoftInput(rg6)
                        val checkedRadioButton = group?.findViewById(group.checkedRadioButtonId) as? RadioButton
                        checkedRadioButton?.let {
                            if (checkedRadioButton.isChecked) {
                                if (checkedRadioButton.id == R.id.rb17) {
                                    relation.confirmed = 1
                                } else if (checkedRadioButton.id == R.id.rb18) {
                                    relation.confirmed = 0
                                }
                            }
                        }
                    }
                }
            }
        }
        // 疑似接触
        val rg = findViewById<RadioGroup>(R.id.rg9)
        relation.touching = 0
        rg.setOnCheckedChangeListener { group, checkedId ->
            KeyboardUtils.hideSoftInput(rg)
            val checkedRadioButton = group?.findViewById(group.checkedRadioButtonId) as? RadioButton
            checkedRadioButton?.let {
                if (checkedRadioButton.isChecked) {
                    if (checkedRadioButton.id == R.id.rb19) {
                        relation.touching = 1
                        pvTime =
                            DialogHelper.showDatePickDialog(this, "日期", Calendar.getInstance(),null, Calendar.getInstance()) { date, _ ->
                                binding.tvTouchTime.text = TimeUtils.date2String(date, "yyyy-MM-dd")
                            }
                        findViewById<LinearLayout>(R.id.ll_visibility7).visibility = View.VISIBLE
                    } else if (checkedRadioButton.id == R.id.rb20) {
                        relation.touching = 0
                        findViewById<LinearLayout>(R.id.ll_visibility7).visibility = View.GONE
                    }
                }
            }
        }
    }
    override fun initViewObservable() {
        // 接触日期选择
        viewModel.uc.choiceBirthdayEvent.observe(this, {
            if (binding.tvTouchTime.text.toString().matches(Regex("^\\d*-\\d*-\\d*\$"))){
                val date = binding.tvTouchTime.text.toString().split("-")
                pvTime.setDate(Calendar.getInstance().apply { this[date[0].toInt(),(date[1].toInt()-1)]=date[2].toInt() })
            }
            KeyboardUtils.hideSoftInput(binding.tvTouchTime)
            pvTime.show()
        })
    }
}